package com.lishangshan.oas_system.controller.role;

import com.github.pagehelper.util.StringUtil;
import com.lishangshan.oas_system.model.dao.roledao.RoleDao;
import com.lishangshan.oas_system.model.dao.roledao.RolepowerlistDao;
import com.lishangshan.oas_system.model.dao.system.SystemMenuDao;
import com.lishangshan.oas_system.model.dao.user.UserDao;
import com.lishangshan.oas_system.model.entity.role.Role;
import com.lishangshan.oas_system.model.entity.role.Rolemenu;
import com.lishangshan.oas_system.model.entity.role.Rolepowerlist;
import com.lishangshan.oas_system.model.entity.user.User;
import com.lishangshan.oas_system.service.role.RoleService;
import com.lishangshan.oas_system.service.role.RolepowerlistService;
import com.lishangshan.oas_system.utils.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 角色 列表 控制器 Controller
 */
@Controller
@RequestMapping("/")
public class RoleController {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RolepowerlistDao rpdao;
    @Autowired
    private RoleService rservice;

    /**
     * 角色 展示 列表
     *
     * @param page
     * @param
     * @return
     */
    @RequestMapping("rolemanage")
    public ModelAndView index(@RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page
    ) {
        Pageable pa = new PageRequest(page, ParameterUtil.PAGE_SIZE_MIN);
        ModelAndView mav = new ModelAndView("role/rolemanage");
        Page<Role> pagerole = roleDao.findAll(pa);
        List<Role> rolelist = pagerole.getContent();
        mav.addObject("page", pagerole);
        mav.addObject("rolelist", rolelist);
        mav.addObject("url", "roleser");
        return mav;
    }

    /**
     * 分页时 调用 roleser 这个方法
     *
     * @param page
     * @param model
     * @param req
     * @return
     */
    @RequestMapping("/roleser")
    public String roleser(@RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page
            , Model model, HttpServletRequest req) {
        PageRequest pa = new PageRequest(page, ParameterUtil.PAGE_SIZE_MIN);
        Page<Role> pagerole = null;
        List<Role> rolelist = null;
        String val = null;
        // val 判断 val 是否为空
        if (!StringUtil.isEmpty(req.getParameter("val"))) {
            val = req.getParameter("val").trim();
        }
        // val 不为 空时 查找 信息
        if (!StringUtil.isEmpty(val)) {
            pagerole = roleDao.findbyrolename(val, pa);
            model.addAttribute("sort", "&val=" + val);
        } else {
            pagerole = roleDao.findAll(pa);
        }
        System.out.println("val = " + val);
        rolelist = pagerole.getContent();
        model.addAttribute("page", pagerole);
        model.addAttribute("rolelist", rolelist);
        model.addAttribute("url", "roleser");
        return "role/roletable";
    }

    /**
     * 角色权限展示
     *
     * @return
     */
    @RequestMapping("roleset")
    public String index2(HttpServletRequest req, Model model) {

        Long roleid = Long.parseLong(req.getParameter("id"));

        Role role = roleDao.findOne(roleid);
        List<Rolemenu> oneMenuAll = rpdao.findbyparentall(0L, roleid);
        List<Rolemenu> twoMenuAll = rpdao.findbyparents(0L, roleid);

        model.addAttribute("oneMenuAll", oneMenuAll);
        model.addAttribute("twoMenuAll", twoMenuAll);
        model.addAttribute("roleid", roleid);
        model.addAttribute("rolename", role.getRoleName());
        return "role/roleset";
    }

    /**
     * 角色权限设定
     */
    @RequestMapping("powerss")
    public @ResponseBody
    Boolean power(HttpServletRequest req) {
        Long roleid = Long.parseLong(req.getParameter("roleid"));
        String content = req.getParameter("content").trim();
        Long menuid = Long.parseLong(req.getParameter("menuid"));
        Rolepowerlist rolepower = rpdao.findbyroleidandmenuid(roleid, menuid);
        System.out.println(rolepower);
        if (content.equals("选中")) {
            rolepower.setCheck(true);
            System.out.println("xuanzhong");
        } else {
            rolepower.setCheck(false);
            System.out.println("not ok");
        }

        rservice.sava(rolepower);
        return true;
    }

/*    // 跳转 添加 and 修改
    @RequestMapping("/addrole")
    public String addrole() {
        return "role/addrole";
    }*/

    // 跳转 添加 and 修改
    @RequestMapping("/addrole")
    public String addrole(@RequestParam(value = "id", defaultValue = ParameterUtil.ROLE_DEFLUT) Long id, Model model) {
        if (id == -1) {
            return "role/addrole";
        } else {
            Role role = roleDao.findOne(id);
            model.addAttribute("role", role);
            return "role/addrole";
        }
    }

    /**
     * 在 删除 角色 列表 时，除了要删除 该角色表 数据之外
     * 还要删除 权限列表 中该角色的权限，and 判断该角色之下
     * 是否还有员工存在
     *
     * @param role_id
     * @param model
     * @return
     */
    @Autowired
    private UserDao udo;

    // 刪除
    @RequestMapping("/deshan")
    public String deshan(@RequestParam("id") Long role_id, Model model) {
        System.out.println(role_id);
        // 根据 role_id 查询 用户表
        List<User> users = udo.findrole(role_id);
        if (users.size() > 0) {
            model.addAttribute("errormess", "该角色下还存在员工,不能删除");
            return "forward:/rolemanage";
        }
        roleDao.delete(role_id);
        model.addAttribute("success", "删除成功");
        return "forward:/rolemanage";
    }

    @Autowired
    private RolepowerlistService rolepowerlistService;
    @Autowired
    private SystemMenuDao systemMenuDao;
    @Autowired
    private RolepowerlistDao rolepowerlistDao;

    // 添加 and 修改 modifyrole
    @RequestMapping("/modifyrole")
    public String modifyrole(Role role, Model model, HttpServletResponse resp) throws IOException {
        // 修改 角色 并不包含 权限
        if (role.getRoleId() != null) {
            Role save = roleDao.save(role);
            if (save != null) {
                model.addAttribute("success", "保存成功");
            } else {
                model.addAttribute("errormess", "保存失败");
            }
        } else {
            // 添加 角色 以及 其权限
            Role save = roleDao.save(role);
            // 获取 此添加 角色 的id
            Long roleId = save.getRoleId();
            // 查询 超级管理园 所有 的父菜单 id
            List<Long> integers = rolepowerlistService.findAllBymenu(1, 1l);
            System.out.println("所有管理员 菜单 = " + integers);
            Rolepowerlist rolepowerlist = new Rolepowerlist();
            System.out.println("integers 的大小 = " + integers.size());
            // 遍历查过来的菜单id
            for (Long i : integers
            ) {
                // 最后 添加 Rolepowerlist  表的 数据
                int add = rolepowerlistService.add(0, i, roleId);
            }
            if (save != null) {
                model.addAttribute("success", "保存成功");
            } else {
                model.addAttribute("errormess", "保存失败");
            }
            System.out.println("打印所有的菜单信息id");

        }
        return "forward:/rolemanage";
    }
}

